package tr.richfacesext.components.chart;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import com.googlecode.charts4j.GChart;

/**
 * 
 * @author mert
 *
 */
public class Chart extends UIOutput {

	private Integer _width;
	private Integer _height;
	private String _onclick;	
	
	public Chart() {
		setRendererType(ChartConstants.DEFAULT_RENDERER);
	}

	public String getFamily() {
		return ChartConstants.COMPONENT_FAMILY;
	}

	public Object saveState(FacesContext context) {
		Object values[] = new Object[4];
		values[0] = super.saveState(context);
		values[1] = _width;
		values[2] = _height;
		values[3] = _onclick;
		
		return ((Object) values);
	}
	
	public void restoreState(FacesContext context, Object state) {
		Object values[] = (Object[]) state;
		super.restoreState(context, values[0]);
		_width = (Integer) values[1];
		_height = (Integer) values[2];
		_onclick = (String) values[3];
	}

	public GChart getChart() {
		if (getValue() == null)
			return null;
		
		if (getValue() instanceof GChart)
			return (GChart) getValue();
		
		throw new IllegalArgumentException("value property of component should be an instance of " + GChart.class);
	}	
	
	public Integer getWidth() {
		if(_width != null )
			return _width;

		ValueBinding vb = getValueBinding("width");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 200;
	}
	public void setWidth(Integer widthValue) {
		_width = widthValue;
	}

	public Integer getHeight() {
		if(_height != null )
			return _height;

		ValueBinding vb = getValueBinding("height");
		return vb != null ? (java.lang.Integer) vb.getValue(getFacesContext()) : 120;
	}
	public void setHeight(Integer heightValue) {
		_height = heightValue;
	}	
	
	public String getOnclick() {
		if(_onclick != null )
			return _onclick;

		ValueBinding vb = getValueBinding("onclick");
		return vb != null ? (java.lang.String) vb.getValue(getFacesContext()) : null;
	}
	public void setOnclick(String onclickValue) {
		_onclick = onclickValue;
	}	
}